using Domain.Entities;
using NHibernate;
using System.Collections.Generic;

namespace Domain.Services
{
    public static class BookmarkService
    {
        /*****************************************************************************************
        * Getters
        * ***************************************************************************************/
        public static Bookmark GetByTopicAndUser(Topic topic, User user)
        {
            ISession session = NHibernateHelper.Instance.CurrentSession;
            return session.CreateQuery("select from Bookmark b where b.Topic.Id = " + topic.Id + " and b.User.Id = " + user.Id).UniqueResult<Bookmark>();
        }

        /*****************************************************************************************
        * Adders
        * ***************************************************************************************/
        public static Bookmark NewBookmark(Topic topic, User user, bool sendEmail)
        {
            ISession session = NHibernateHelper.Instance.CurrentSession;
            using (ITransaction transaction = session.BeginTransaction())
            {
                Bookmark bookmark = new Bookmark();
                bookmark.Topic = topic;
                bookmark.User = user;
                bookmark.SendEmail = sendEmail;
                session.SaveOrUpdate(bookmark);

                transaction.Commit();

                return bookmark;
            }
        }

        /*****************************************************************************************
        * Updaters
        * ***************************************************************************************/
        public static void Update(Bookmark bookmark, bool sendEmail)
        {
            ISession session = NHibernateHelper.Instance.CurrentSession;
            using (ITransaction transaction = session.BeginTransaction())
            {
                bookmark.SendEmail = sendEmail;

                session.SaveOrUpdate(bookmark);
                transaction.Commit();
            }
        }

        /*****************************************************************************************
        * Deleters
        * ***************************************************************************************/
    }
}
